home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_407 / flex / src.lzh / src / misc.c < prev    next >
C/C++ Source or Header  |  1990-07-14  |  14KB  |  786 lines

  1. /* misc - miscellaneous flex routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] =
  31.     "@(#) $Header: WPL:Generators/flex-2.3/RCS/misc.c,v 1.2 90/07/15 01:18:35 loftus Exp $ (LBL)";
  32. #endif
  33.  
  34. #include <ctype.h>
  35. #include "flexdef.h"
  36.  
  37.  
  38. /* ANSI C does not guarantee that isascii() is defined */
  39. #ifndef isascii
  40. #define isascii(c) ((c) <= 0177)
  41. #endif
  42.  
  43.  
  44.  
  45. /* declare functions that have forward references */
  46.  
  47. void dataflush PROTO(());
  48. int otoi PROTO((Char []));
  49.  
  50.  
  51. /* action_out - write the actions from the temporary file to lex.yy.c
  52.  *
  53.  * synopsis
  54.  *     action_out();
  55.  *
  56.  *     Copies the action file up to %% (or end-of-file) to lex.yy.c
  57.  */
  58.  
  59. void action_out()
  60.  
  61.     {
  62.     char buf[MAXLINE];
  63.  
  64.     while ( fgets( buf, MAXLINE, temp_action_file ) != NULL )
  65.     if ( buf[0] == '%' && buf[1] == '%' )
  66.         break;
  67.     else
  68.         fputs( buf, stdout );
  69.     }
  70.  
  71.  
  72. /* allocate_array - allocate memory for an integer array of the given size */
  73.  
  74. void *allocate_array( size, element_size )
  75. int size, element_size;
  76.  
  77.     {
  78.     register void *mem;
  79.  
  80.     /* on 16-bit int machines (e.g., 80286) we might be trying to
  81.      * allocate more than a signed int can hold, and that won't
  82.      * work.  Cheap test:
  83.      */
  84.     if ( element_size * size <= 0 )
  85.         flexfatal( "request for < 1 byte in allocate_array()" );
  86.  
  87.     mem = (void *) malloc( (unsigned) (element_size * size) );
  88.  
  89.     if ( mem == NULL )
  90.     flexfatal( "memory allocation failed in allocate_array()" );
  91.  
  92.     return ( mem );
  93.     }
  94.  
  95.  
  96. /* all_lower - true if a string is all lower-case
  97.  *
  98.  * synopsis:
  99.  *    Char *str;
  100.  *    int all_lower();
  101.  *    true/false = all_lower( str );
  102.  */
  103.  
  104. int all_lower( str )
  105. register Char *str;
  106.  
  107.     {
  108.     while ( *str )
  109.     {
  110.     if ( ! isascii( *str ) || ! islower( *str ) )
  111.         return ( 0 );
  112.     ++str;
  113.     }
  114.  
  115.     return ( 1 );
  116.     }
  117.  
  118.  
  119. /* all_upper - true if a string is all upper-case
  120.  *
  121.  * synopsis:
  122.  *    Char *str;
  123.  *    int all_upper();
  124.  *    true/false = all_upper( str );
  125.  */
  126.  
  127. int all_upper( str )
  128. register Char *str;
  129.  
  130.     {
  131.     while ( *str )
  132.     {
  133.     if ( ! isascii( *str ) || ! isupper( (char) *str ) )
  134.         return ( 0 );
  135.     ++str;
  136.     }
  137.  
  138.     return ( 1 );
  139.     }
  140.  
  141.  
  142. /* bubble - bubble sort an integer array in increasing order
  143.  *
  144.  * synopsis
  145.  *   int v[n], n;
  146.  *   bubble( v, n );
  147.  *
  148.  * description
  149.  *   sorts the first n elements of array v and replaces them in
  150.  *   increasing order.
  151.  *
  152.  * passed
  153.  *   v - the array to be sorted
  154.  *   n - the number of elements of 'v' to be sorted */
  155.  
  156. void bubble( v, n )
  157. int v[], n;
  158.  
  159.     {
  160.     register int i, j, k;
  161.  
  162.     for ( i = n; i > 1; --i )
  163.     for ( j = 1; j < i; ++j )
  164.         if ( v[j] > v[j + 1] )    /* compare */
  165.         {
  166.         k = v[j];    /* exchange */
  167.         v[j] = v[j + 1];
  168.         v[j + 1] = k;
  169.         }
  170.     }
  171.  
  172.  
  173. /* clower - replace upper-case letter to lower-case
  174.  *
  175.  * synopsis:
  176.  *    Char clower();
  177.  *    int c;
  178.  *    c = clower( c );
  179.  */
  180.  
  181. Char clower( c )
  182. register int c;
  183.  
  184.     {
  185.     return ( (isascii( c ) && isupper( c )) ? tolower( c ) : c );
  186.     }
  187.  
  188.  
  189. /* copy_string - returns a dynamically allocated copy of a string
  190.  *
  191.  * synopsis
  192.  *    char *str, *copy, *copy_string();
  193.  *    copy = copy_string( str );
  194.  */
  195.  
  196. char *copy_string( str )
  197. register char *str;
  198.  
  199.     {
  200.     register char *c;
  201.     char *copy;
  202.  
  203.     /* find length */
  204.     for ( c = str; *c; ++c )
  205.     ;
  206.  
  207.     copy = malloc( (unsigned) ((c - str + 1) * sizeof( char )) );
  208.  
  209.     if ( copy == NULL )
  210.     flexfatal( "dynamic memory failure in copy_string()" );
  211.  
  212.     for ( c = copy; (*c++ = *str++); )
  213.     ;
  214.  
  215.     return ( copy );
  216.     }
  217.  
  218.  
  219. /* copy_unsigned_string -
  220.  *    returns a dynamically allocated copy of a (potentially) unsigned string
  221.  *
  222.  * synopsis
  223.  *    Char *str, *copy, *copy_unsigned_string();
  224.  *    copy = copy_unsigned_string( str );
  225.  */
  226.  
  227. Char *copy_unsigned_string( str )
  228. register Char *str;
  229.  
  230.     {
  231.     register Char *c;
  232.     Char *copy;
  233.  
  234.     /* find length */
  235.     for ( c = str; *c; ++c )
  236.     ;
  237.  
  238.     copy = (Char *) malloc( (unsigned) ((c - str + 1) * sizeof( Char )) );
  239.  
  240.     if ( copy == NULL )
  241.     flexfatal( "dynamic memory failure in copy_unsigned_string()" );
  242.  
  243.     for ( c = copy; (*c++ = *str++); )
  244.     ;
  245.  
  246.     return ( copy );
  247.     }
  248.  
  249.  
  250. /* cshell - shell sort a character array in increasing order
  251.  *
  252.  * synopsis
  253.  *
  254.  *   Char v[n];
  255.  *   int n, special_case_0;
  256.  *   cshell( v, n, special_case_0 );
  257.  *
  258.  * description
  259.  *   does a shell sort of the first n elements of array v.
  260.  *   If special_case_0 is true, then any element equal to 0
  261.  *   is instead assumed to have infinite weight.
  262.  *
  263.  * passed
  264.  *   v - array to be sorted
  265.  *   n - number of elements of v to be sorted
  266.  */
  267.  
  268. void cshell( v, n, special_case_0 )
  269. Char v[];
  270. int n, special_case_0;
  271.  
  272.     {
  273.     int gap, i, j, jg;
  274.     Char k;
  275.  
  276.     for ( gap = n / 2; gap > 0; gap = gap / 2 )
  277.     for ( i = gap; i < n; ++i )
  278.         for ( j = i - gap; j >= 0; j = j - gap )
  279.         {
  280.         jg = j + gap;
  281.  
  282.         if ( special_case_0 )
  283.             {
  284.             if ( v[jg] == 0 )
  285.             break;
  286.  
  287.             else if ( v[j] != 0 && v[j] <= v[jg] )
  288.             break;
  289.             }
  290.  
  291.         else if ( v[j] <= v[jg] )
  292.             break;
  293.  
  294.         k = v[j];
  295.         v[j] = v[jg];
  296.         v[jg] = k;
  297.         }
  298.     }
  299.  
  300.  
  301. /* dataend - finish up a block of data declarations
  302.  *
  303.  * synopsis
  304.  *    dataend();
  305.  */
  306.  
  307. void dataend()
  308.  
  309.     {
  310.     if ( datapos > 0 )
  311.     dataflush();
  312.  
  313.     /* add terminator for initialization */
  314.     puts( "    } ;\n" );
  315.  
  316.     dataline = 0;
  317.     datapos = 0;
  318.     }
  319.  
  320.  
  321.  
  322. /* dataflush - flush generated data statements
  323.  *
  324.  * synopsis
  325.  *    dataflush();
  326.  */
  327.  
  328. void dataflush()
  329.  
  330.     {
  331.     putchar( '\n' );
  332.  
  333.     if ( ++dataline >= NUMDATALINES )
  334.     {
  335.     /* put out a blank line so that the table is grouped into
  336.      * large blocks that enable the user to find elements easily
  337.      */
  338.     putchar( '\n' );
  339.     dataline = 0;
  340.     }
  341.  
  342.     /* reset the number of characters written on the current line */
  343.     datapos = 0;
  344.     }
  345.  
  346.  
  347. /* flexerror - report an error message and terminate
  348.  *
  349.  * synopsis
  350.  *    char msg[];
  351.  *    flexerror( msg );
  352.  */
  353.  
  354. void flexerror( msg )
  355. char msg[];
  356.  
  357.     {
  358.     fprintf( stderr, "%s: %s\n", program_name, msg );
  359.  
  360.     flexend( 1 );
  361.     }
  362.  
  363.  
  364. /* flexfatal - report a fatal error message and terminate
  365.  *
  366.  * synopsis
  367.  *    char msg[];
  368.  *    flexfatal( msg );
  369.  */
  370.  
  371. void flexfatal( msg )
  372. char msg[];
  373.  
  374.     {
  375.     fprintf( stderr, "%s: fatal internal error, %s\n", program_name, msg );
  376.     flexend( 1 );
  377.     }
  378.  
  379.  
  380. /* flex_gettime - return current time
  381.  *
  382.  * synopsis
  383.  *    char *flex_gettime(), *time_str;
  384.  *    time_str = flex_gettime();
  385.  *
  386.  * note
  387.  *    the routine name has the "flex_" prefix because of name clashes
  388.  *    with Turbo-C
  389.  */
  390.  
  391. /* include sys/types.h to use time_t and make lint happy */
  392.  
  393. #ifndef MS_DOS
  394. #ifndef VMS
  395. #include <sys/types.h>
  396. #else
  397. #include <types.h>
  398. #endif
  399. #endif
  400.  
  401. #ifdef MS_DOS
  402. #include <time.h>
  403. typedef long time_t;
  404. #endif
  405.  
  406. #ifdef AMIGA
  407. #include <time.h>
  408. #endif
  409.  
  410. char *flex_gettime()
  411.  
  412.     {
  413.     time_t t, time();
  414.     char *result, *ctime(), *copy_string();
  415.  
  416.     t = time( (long *) 0 );
  417.  
  418.     result = copy_string( ctime( &t ) );
  419.  
  420.     /* get rid of trailing newline */
  421.     result[24] = '\0';
  422.  
  423.     return ( result );
  424.     }
  425.  
  426.  
  427. /* lerrif - report an error message formatted with one integer argument
  428.  *
  429.  * synopsis
  430.  *    char msg[];
  431.  *    int arg;
  432.  *    lerrif( msg, arg );
  433.  */
  434.  
  435. void lerrif( msg, arg )
  436. char msg[];
  437. int arg;
  438.  
  439.     {
  440.     char errmsg[MAXLINE];
  441.     (void) sprintf( errmsg, msg, arg );
  442.     flexerror( errmsg );
  443.     }
  444.  
  445.  
  446. /* lerrsf - report an error message formatted with one string argument
  447.  *
  448.  * synopsis
  449.  *    char msg[], arg[];
  450.  *    lerrsf( msg, arg );
  451.  */
  452.  
  453. void lerrsf( msg, arg )
  454. char msg[], arg[];
  455.  
  456.     {
  457.     char errmsg[MAXLINE];
  458.  
  459.     (void) sprintf( errmsg, msg, arg );
  460.     flexerror( errmsg );
  461.     }
  462.  
  463.  
  464. /* htoi - convert a hexadecimal digit string to an integer value
  465.  *
  466.  * synopsis:
  467.  *    int val, htoi();
  468.  *    Char str[];
  469.  *    val = htoi( str );
  470.  */
  471.  
  472. int htoi( str )
  473. Char str[];
  474.  
  475.     {
  476.     int result;
  477.  
  478.     (void) sscanf( (char *) str, "%x", &result );
  479.  
  480.     return ( result );
  481.     }
  482.  
  483.  
  484. /* line_directive_out - spit out a "# line" statement */
  485.  
  486. void line_directive_out( output_file_name )
  487. FILE *output_file_name;
  488.  
  489.     {
  490.     if ( infilename && gen_line_dirs )
  491.         fprintf( output_file_name, "# line %d \"%s\"\n", linenum, infilename );
  492.     }
  493.  
  494.  
  495. /* mk2data - generate a data statement for a two-dimensional array
  496.  *
  497.  * synopsis
  498.  *    int value;
  499.  *    mk2data( value );
  500.  *
  501.  *  generates a data statement initializing the current 2-D array to "value"
  502.  */
  503. void mk2data( value )
  504. int value;
  505.  
  506.     {
  507.     if ( datapos >= NUMDATAITEMS )
  508.     {
  509.     putchar( ',' );
  510.     dataflush();
  511.     }
  512.  
  513.     if ( datapos == 0 )
  514.     /* indent */
  515.     fputs( "    ", stdout );
  516.  
  517.     else
  518.     putchar( ',' );
  519.  
  520.     ++datapos;
  521.  
  522.     printf( "%5d", value );
  523.     }
  524.  
  525.  
  526. /* mkdata - generate a data statement
  527.  *
  528.  * synopsis
  529.  *    int value;
  530.  *    mkdata( value );
  531.  *
  532.  *  generates a data statement initializing the current array element to
  533.  *  "value"
  534.  */
  535. void mkdata( value )
  536. int value;
  537.  
  538.     {
  539.     if ( datapos >= NUMDATAITEMS )
  540.     {
  541.     putchar( ',' );
  542.     dataflush();
  543.     }
  544.  
  545.     if ( datapos == 0 )
  546.     /* indent */
  547.     fputs( "    ", stdout );
  548.  
  549.     else
  550.     putchar( ',' );
  551.  
  552.     ++datapos;
  553.  
  554.     printf( "%5d", value );
  555.     }
  556.  
  557.  
  558. /* myctoi - return the integer represented by a string of digits
  559.  *
  560.  * synopsis
  561.  *    Char array[];
  562.  *    int val, myctoi();
  563.  *    val = myctoi( array );
  564.  *
  565.  */
  566.  
  567. int myctoi( array )
  568. Char array[];
  569.  
  570.     {
  571.     int val = 0;
  572.  
  573.     (void) sscanf( (char *) array, "%d", &val );
  574.  
  575.     return ( val );
  576.     }
  577.  
  578.  
  579. /* myesc - return character corresponding to escape sequence
  580.  *
  581.  * synopsis
  582.  *    Char array[], c, myesc();
  583.  *    c = myesc( array );
  584.  *
  585.  */
  586.  
  587. Char myesc( array )
  588. Char array[];
  589.  
  590.     {
  591.     switch ( array[1] )
  592.     {
  593.     case 'a': return ( '\a' );
  594.     case 'b': return ( '\b' );
  595.     case 'f': return ( '\f' );
  596.     case 'n': return ( '\n' );
  597.     case 'r': return ( '\r' );
  598.     case 't': return ( '\t' );
  599.     case 'v': return ( '\v' );
  600.  
  601.     case 'x':
  602.         /* fall through */
  603.  
  604.     case '0':
  605.     case '1':
  606.     case '2':
  607.     case '3':
  608.     case '4':
  609.     case '5':
  610.     case '6':
  611.     case '7':
  612.     case '8':
  613.     case '9':
  614.  
  615.         { /* \<octal> or \x<hex> */
  616.         Char c, esc_char;
  617.         register int sptr = 1;
  618.  
  619.         if ( array[1] == 'x' )
  620.         ++sptr;
  621.  
  622.         while ( isascii( array[sptr] ) && isdigit( array[sptr] ) )
  623.         /* don't increment inside loop control because if
  624.          * isdigit() is a macro it will expand it to two
  625.          * increments ...
  626.          */
  627.         ++sptr;
  628.  
  629.         c = array[sptr];
  630.         array[sptr] = '\0';
  631.  
  632.         if ( array[1] == 'x' )
  633.         esc_char = htoi( array + 2 );
  634.         else
  635.         esc_char = otoi( array + 1 );
  636.  
  637.         array[sptr] = c;
  638.  
  639.         return ( esc_char );
  640.         }
  641.  
  642.     default:
  643.         return ( array[1] );
  644.     }
  645.     }
  646.  
  647.  
  648. /* otoi - convert an octal digit string to an integer value
  649.  *
  650.  * synopsis:
  651.  *    int val, otoi();
  652.  *    Char str[];
  653.  *    val = otoi( str );
  654.  */
  655.  
  656. int otoi( str )
  657. Char str[];
  658.  
  659.     {
  660.     int result;
  661.  
  662.     (void) sscanf( (char *) str, "%o", &result );
  663.  
  664.     return ( result );
  665.     }
  666.  
  667.  
  668. /* readable_form - return the the human-readable form of a character
  669.  *
  670.  * synopsis:
  671.  *    int c;
  672.  *    char *readable_form();
  673.  *    <string> = readable_form( c );
  674.  *
  675.  * The returned string is in static storage.
  676.  */
  677.  
  678. char *readable_form( c )
  679. register int c;
  680.  
  681.     {
  682.     static char rform[10];
  683.  
  684.     if ( (c >= 0 && c < 32) || c >= 127 )
  685.     {
  686.     switch ( c )
  687.         {
  688.         case '\n': return ( "\\n" );
  689.         case '\t': return ( "\\t" );
  690.         case '\f': return ( "\\f" );
  691.         case '\r': return ( "\\r" );
  692.         case '\b': return ( "\\b" );
  693.  
  694.         default:
  695.         (void) sprintf( rform, "\\%.3o", c );
  696.         return ( rform );
  697.         }
  698.     }
  699.  
  700.     else if ( c == ' ' )
  701.     return ( "' '" );
  702.  
  703.     else
  704.     {
  705.     rform[0] = c;
  706.     rform[1] = '\0';
  707.  
  708.     return ( rform );
  709.     }
  710.     }
  711.  
  712.  
  713. /* reallocate_array - increase the size of a dynamic array */
  714.  
  715. void *reallocate_array( array, size, element_size )
  716. void *array;
  717. int size, element_size;
  718.  
  719.     {
  720.     register void *new_array;
  721.  
  722.     /* same worry as in allocate_array(): */
  723.     if ( size * element_size <= 0 )
  724.         flexfatal( "attempt to increase array size by less than 1 byte" );
  725.  
  726.     new_array =
  727.     (void *) realloc( (char *)array, (unsigned) (size * element_size ));
  728.  
  729.     if ( new_array == NULL )
  730.     flexfatal( "attempt to increase array size failed" );
  731.  
  732.     return ( new_array );
  733.     }
  734.  
  735.  
  736. /* skelout - write out one section of the skeleton file
  737.  *
  738.  * synopsis
  739.  *    skelout();
  740.  *
  741.  * DESCRIPTION
  742.  *    Copies from skelfile to stdout until a line beginning with "%%" or
  743.  *    EOF is found.
  744.  */
  745. void skelout()
  746.  
  747.     {
  748.     char buf[MAXLINE];
  749.  
  750.     while ( fgets( buf, MAXLINE, skelfile ) != NULL )
  751.     if ( buf[0] == '%' && buf[1] == '%' )
  752.         break;
  753.     else
  754.         fputs( buf, stdout );
  755.     }
  756.  
  757.  
  758. /* transition_struct_out - output a yy_trans_info structure
  759.  *
  760.  * synopsis
  761.  *     int element_v, element_n;
  762.  *     transition_struct_out( element_v, element_n );
  763.  *
  764.  * outputs the yy_trans_info structure with the two elements, element_v and
  765.  * element_n.  Formats the output with spaces and carriage returns.
  766.  */
  767.  
  768. void transition_struct_out( element_v, element_n )
  769. int element_v, element_n;
  770.  
  771.     {
  772.     printf( "%7d, %5d,", element_v, element_n );
  773.  
  774.     datapos += TRANS_STRUCT_PRINT_LENGTH;
  775.  
  776.     if ( datapos >= 75 )
  777.     {
  778.     putchar( '\n' );
  779.  
  780.     if ( ++dataline % 10 == 0 )
  781.         putchar( '\n' );
  782.  
  783.     datapos = 0;
  784.     }
  785.     }
  786.